﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    [Route("role")]
    [Authorize]
    public class RoleController : Controller
    {

        private readonly IRepository<Role, Guid> _roleRepository;
        private readonly IRoleService _roleService;
        private readonly IModuleService _moduleService;


        public RoleController(IRepository<Role, Guid> roleRepository, IRoleService roleService, IModuleService moduleService)
        {
            _roleRepository = roleRepository;
            _roleService = roleService;
            _moduleService = moduleService;
        }

        [HttpGet("")]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("")]
        public async Task<IActionResult> Index(RoleQueryModel model)
        {
            var userInput = new GetRoleInput { CurrentPage = model.Page, MaxResultCount = model.Limit, RoleName = model.RoleName, Key = model.Key };
            var data = await _roleService.GetPaginatedResult(userInput);
            return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
        }

        [HttpGet("fmodule/{roleId}")]
        public IActionResult FenPeiModule(Guid roleId)
        {
            var role = _roleService.GetRole(roleId);
            var modules = _moduleService.GetModuleCategories();

            RoleModel rm = new RoleModel { Role = role, ModuleCategories = modules };

            return View(rm);
        }

        [HttpPost("fmodule")]
        public IActionResult FenPeiModule(Guid roleId, Guid[] modules)
        {
            _roleService.FenPeiModule(roleId, modules);
            return Json(new { success = 1, msg = "分配成功" });
        }

        [HttpGet("add")]
        public IActionResult Add()
        {
            return View();
        }

        [HttpPost("add")]
        public IActionResult Add(RoleAddAndUpdateModel model)
        {
            _roleRepository.Insert(new Role
            {
                Keys = model.Keys,
                Name = model.Name,
                RoleID = Guid.NewGuid()
            });

            return Json(new { success = 1, msg = "添加成功" });
        }

        [HttpGet("edit/{roleId}")]
        public IActionResult Edit(Guid roleId)
        {
            var r = _roleRepository.FirstOrDefault(x => x.RoleID == roleId);
            return View(r);
        }

        [HttpPost("edit")]
        public IActionResult edit(RoleAddAndUpdateModel model)
        {
            var r = _roleRepository.FirstOrDefault(x => x.RoleID == model.RoleID);

            r.Name = model.Name;
            r.Keys = model.Keys;

            _roleRepository.Update(r);
            
            return Json(new { success = 1, msg = "修改成功" });
        }


        [HttpPost("del/{id}")]
        public IActionResult Del(Guid id)
        {

            Role r = new Role { RoleID = id };
            _roleRepository.Delete(r);
            return Json(new { success = 1, msg = "删除成功" });
        }
    }
}
